package com.yzy.soft.system.controller.page;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.yzy.soft.common.persistence.dao.MetadataColumnMapper;
import com.yzy.soft.common.persistence.model.MetadataColumn;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.system.freemarker.SynchronousMetadata;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
* 描述：测试控制层
* @author Yinzhiyuan
* @date 2018-12-18 14:54:01
*/
@Api(value="测试控制层")
@RestController
@RequestMapping("/pagemetadatacolumn")
public class PageMetadataColumnController extends BaseController{

    //@Autowired
    //private IMetadataColumnControllerService metadataColumnControllerService;

    @Autowired
    private MetadataColumnMapper metadataColumnMapper;
    @Autowired
    private SynchronousMetadata synchronousMetadata;


    /**
    * 描述：查询 测试列表
    */
    @ApiOperation(value="查询测试")
    @GetMapping(value = "/page/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize)throws Exception {

        Wrapper<MetadataColumn> metadataColumnWrapper=new EntityWrapper<MetadataColumn>();
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<MetadataColumn> result=metadataColumnMapper.selectPage(new RowBounds(page,pageSize),metadataColumnWrapper);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(metadataColumnMapper.selectCount(metadataColumnWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    /**
    * 描述：根据Id 查询 测试
    * @param id  测试id
    */
    @ApiOperation(value="查询测试")
    @GetMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public MetadataColumn findById(@PathVariable("id") Integer id)throws Exception {
        return metadataColumnMapper.selectById(id);
    }

    /**
    * 描述:创建测试
    * @param metadataColumnControllerDTO  测试DTO
    */
    @ApiOperation(value="创建测试")
    @PostMapping("")
    public Tip create(@ModelAttribute MetadataColumn metadataColumn) {
        metadataColumn.setCreatetime(new Date());
        metadataColumn.setLastupdatetime(new Date());
        Integer result=metadataColumnMapper.insert(metadataColumn);
        return SUCCESS_TIP;
    }

   /**
    * 描述：删除测试
    * @param id 测试id
    */
    @ApiOperation(value="删除测试")
    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name="ids[]",required = false) String[] metadataColumnController,@PathVariable(name="id",required = false) String id) {
        //Collections.<String>
        if(StringUtils.isNotBlank(id)){
            Integer result=metadataColumnMapper.deleteById(id);
        }else if(metadataColumnController.length!=0){
            Integer result=metadataColumnMapper.deleteBatchIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation(value="批量删除测试")
    @DeleteMapping("/")
    public Tip delete1(@RequestParam(name="ids[]",required = false) String[] metadataColumnController) {
        if(metadataColumnController.length!=0){
           Integer result=metadataColumnMapper.deleteBatchIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }


    /**
    * 描述：更新测试
    * @param id 测试id
    */
    @ApiOperation(value="更新测试")
    @PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip updateMetadataColumnController(@ModelAttribute MetadataColumn metadataColumn) throws Exception {
        metadataColumn.setLastupdatetime(new Date());
        Integer result=metadataColumnMapper.updateById(metadataColumn);
        return SUCCESS_TIP;
    }

    @GetMapping("/by")
    public TableDataDto getCadtion(@ModelAttribute MetadataColumn metadataColumn) {
        Wrapper<MetadataColumn> wrapper = new EntityWrapper<MetadataColumn>();
        wrapper.where("table_id={0}", metadataColumn.getTableId());
        List<MetadataColumn> result = metadataColumnMapper.selectList(wrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(result.size());
        tableDataDto.setData(result);
        return tableDataDto;
    }

    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id")Integer id) {
        Integer result=metadataColumnMapper.status(id);
        if(null==result){
           return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }

    @GetMapping("/metadataColumnControllerId/{metadataColumnControllerId}")
    public TableDataDto getbyAccountId(@PathVariable(name="metadataColumnControllerId") String metadataColumnControllerId,@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
       Wrapper<MetadataColumn> metadataColumnControllerWrapper=new EntityWrapper<>();
       metadataColumnControllerWrapper.where("accountid={0}",metadataColumnControllerId);
       page=(page-1)*pageSize;
       pageSize=page+pageSize;
       List<MetadataColumn> result=metadataColumnMapper.selectPage(new RowBounds(page,pageSize),metadataColumnControllerWrapper);
       TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(metadataColumnMapper.selectCount(metadataColumnControllerWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }
    @ApiOperation("批量将费用归档设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name="ids[]",required = false) Integer[] metadataColumnController) {
        if(metadataColumnController.length!=0){
           Integer result=metadataColumnMapper.batchNuUseIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量将费用归档设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name="ids[]",required = false) Integer[] metadataColumnController) {
        if(metadataColumnController.length!=0){
            Integer result=metadataColumnMapper.batchUseIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }
    @GetMapping("/synchronous")
    public Tip synchronous() {

//        SynchronousMetadata synchronousMetadata=new SynchronousMetadata();
        try {
            synchronousMetadata.synchronousMetadataColum();
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorTip(500,e.getMessage());
        }
//        if(null==result){
//            return new ErrorTip(500,"s");
//        }
        return SUCCESS_TIP;
    }

}